package com.wjr.braveheart.ui.activity;

import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;

import com.wjr.braveheart.eventbus.EventBusUtils;
import com.wjr.braveheart.eventbus.MessageEvent;
import com.wjr.braveheart.utils.ActivityManagerUtils;
import com.wjr.braveheart.utils.StatusBarUtils;
import com.wjr.braveheart.utils.ToastUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 与UI有关的Activity基本设置
 */
public abstract class AppActivity extends BaseActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManagerUtils.getInstance().attachActivity(this);
        if (isRegisterEventBus()) {
            EventBusUtils.register(this);
        }
        setStatusBar();
//*****************************************App 黑白化***********************************************
//        Paint paint = new Paint();
//        ColorMatrix cm = new ColorMatrix();
//        cm.setSaturation(0);
//        paint.setColorFilter(new ColorMatrixColorFilter(cm));
//        getWindow().getDecorView().setLayerType(View.LAYER_TYPE_HARDWARE, paint);
//*****************************************App 黑白化***********************************************
    }

    /**
     * 设置状态栏颜色
     */
    protected void setStatusBar() {
        StatusBarUtils.setTranslucentDiff(this);
    }

    /**
     * 进行解绑操作
     */
    public void unbindView() {
        if (isRegisterEventBus()) {
            EventBusUtils.unregister(this);
        }
        ActivityManagerUtils.getInstance().detachActivity(this);
        ToastUtils.cancelToast();
    }

    /**
     * 需要接收事件 重写该方法 并返回true
     */
    protected boolean isRegisterEventBus() {
        return true;
    }

    /**
     * Called in Android UI's main thread
     * 子类接受事件 重写该方法
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBus(MessageEvent event) {
    }

    /**
     * Called in a separate thread
     * 异步执行，另起线程操作。
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.ASYNC)
    public void onEventAsync(MessageEvent event) {
    }

    /**
     * Called in the background thread
     * 后台进程，处理如保存到数据库等操作。
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void onEventBackground(MessageEvent event) {
    }

    /**
     * Called in the same thread (default)
     * 在同一个线程中执行
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onEventPosting(MessageEvent event) {
    }
}
